<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\user;
use Hash;
class UserLoginController extends Controller
{
    /**
     * 显示后台的登录页面
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('admins.login');
    }

    /**
     * 执行后台登录操作
     */
    public function doLogin(Request $request)
    {
    	//获取用户的信息
   		$username = $request->input('username');
   		//检索数据库
   		$res = User::where('username',$username)->first();
   		if (empty($res)) {
   			return back()->with('info','用户名不存在');
   		}
   		// dd($res->password);
   		//检测用户密码
	  	if (Hash::check($request->password,$res->password)) {

	  		  session(['user'=>$res->username]);
	  		  return redirect('/users/user-list')->with('info','登陆成功');
		}else{
			return back()->with('info','密码不对');
		}
    }
    /**
     * 退出登录
     */
    public function loginOut(Request $request)
    {
      $request->session()->flush();
    	return redirect('/login');
    }
}
